#include "start_page.h"
static lv_style_t style_bg;
static lv_style_t style_indic;
static lv_obj_t *bar;
static lv_obj_t* label;
static const lv_font_t* font_small;
static lv_anim_timeline_t* at;
static lv_style_t font_style;
lv_obj_t* grad;
#define MASK_WIDTH 200
#define MASK_HEIGHT 90
static void set_value(void* bar, int32_t v)
{

    if (v <= 100)
    {
        lv_bar_set_value(bar, v, LV_ANIM_ON);
        
    }
    else if (v == 500)
    {
        printf("load main_page");
        lv_page_load(WEATHER_PAGE);
    }
}

void lv_bar_show(struct _lv_anim_t* ani)
{
    lv_obj_clear_flag(bar, LV_OBJ_FLAG_HIDDEN);
}

static void add_mask_event_cb1212(lv_event_t* e)
{
    static lv_draw_mask_map_param_t m;
    static int16_t mask_id;

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* obj = lv_event_get_target(e);
    lv_opa_t* mask_map = lv_event_get_user_data(e);
    if (code == LV_EVENT_COVER_CHECK) {
        lv_event_set_cover_res(e, LV_COVER_RES_MASKED);
    }
    else if (code == LV_EVENT_DRAW_MAIN_BEGIN) {
        lv_draw_mask_map_init(&m, &obj->coords, mask_map);
        mask_id = lv_draw_mask_add(&m, NULL);

    }
    else if (code == LV_EVENT_DRAW_MAIN_END) {
        lv_draw_mask_free_param(&m);
        lv_draw_mask_remove_id(mask_id);
    }
}

void boot_ainmation(lv_obj_t *obj)
{
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);

    //2、创建动画b
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_exec_cb(&a, lv_obj_set_y);
    lv_anim_set_ready_cb(&a, lv_bar_show);
    lv_anim_set_var(&a, label);
    lv_anim_set_values(&a, -20, 80);
    lv_anim_set_time(&a, 500);
    lv_anim_set_path_cb(&a, lv_anim_path_bounce);

    lv_anim_t b;
    lv_anim_init(&b);
    lv_anim_set_exec_cb(&b, lv_obj_set_y);
    lv_anim_set_var(&b, bar);
    lv_anim_set_values(&b, -40, 118);
    lv_anim_set_time(&b, 1000);
    lv_anim_set_path_cb(&b, lv_anim_path_bounce);

        //1、创建动画a
    lv_anim_t c;
    lv_anim_init(&c);
    lv_anim_set_var(&c, bar);
    lv_anim_set_values(&c, -20, 500);
    lv_anim_set_time(&c, 2000);
    lv_anim_set_exec_cb(&c, set_value);
    lv_anim_set_path_cb(&c, lv_anim_path_ease_in);
    //3、添加动画组并启动
    at = lv_anim_timeline_create();
    lv_anim_timeline_add(at, 0, &a);
    lv_anim_timeline_add(at, 1200, &b);
    lv_anim_timeline_add(at, 2400, &c);

    lv_anim_timeline_start(at);
}


void start_page(lv_obj_t* obj)
{
    printf("start_page");

    lv_obj_set_size(obj, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(obj,lv_color_black(),0);
    lv_style_init(&style_bg);
    lv_style_set_border_color(&style_bg, lv_palette_main(LV_PALETTE_ORANGE));
    lv_style_set_border_width(&style_bg, 2);
    lv_style_set_pad_all(&style_bg, 6); /*To make the indicator smaller*/
    lv_style_set_radius(&style_bg, 6);
    lv_style_set_anim_time(&style_bg, 1000);

    lv_style_init(&style_indic);
    lv_style_set_bg_opa(&style_indic, LV_OPA_COVER);
    lv_style_set_bg_color(&style_indic, lv_palette_main(LV_PALETTE_ORANGE));
    lv_style_set_radius(&style_indic, 3);

    //bar
    bar = lv_bar_create(obj);
    lv_obj_remove_style_all(bar); /*To have a clean start*/
    lv_obj_set_size(bar, 200, 20);
    lv_obj_set_pos(bar,8,300);
    lv_obj_add_flag(bar, LV_OBJ_FLAG_HIDDEN);
    bar->user_data = (void*)obj;
    lv_obj_add_style(bar, &style_bg, 0);
    lv_obj_add_style(bar, &style_indic, LV_PART_INDICATOR);
    LV_FONT_DECLARE(myfont);

    //label
    lv_style_init(&font_style);
    lv_style_set_text_color(&font_style, lv_palette_main(LV_PALETTE_ORANGE));
    label = lv_label_create(obj);

    lv_obj_set_x(label, 15);
    font_small = &lv_font_montserrat_32;
    lv_label_set_text(label, "Tiny Watch");
    lv_obj_set_style_text_font(label, font_small, 0);

    lv_obj_add_style(label,&font_style,0);

}

void start_destroy(lv_obj_t* obj)
{
    printf("start_destroy");
    lv_style_reset(&style_bg);
    lv_style_reset(&style_indic);

}
